﻿using UnityEngine;
using UnityEngine.Events;

namespace KuiHuaBaoDian {

    [RequireComponent(typeof(ParticleSystem))]
    [DisallowMultipleComponent]
    public sealed class ParticleSystemListener : MonoBehaviour {

        private ParticleSystemListener() : base() { }

        public UnityEvent onComplete = new();

        private void Start() {
            var particleSystem = GetComponent<ParticleSystem>();
            var mainModule = particleSystem.main;
            mainModule.stopAction = ParticleSystemStopAction.Callback;
        }

        private void OnParticleSystemStopped() {
            onComplete.Invoke();
        }

        private void OnDestroy() {
            onComplete.RemoveAllListeners();
            onComplete = null;
        }
    }
}
